package com.bicon.botu.mall.order.api;

import com.bicon.botu.mall.order.common.OrderConstans;
import com.bicon.botu.mall.order.entity.GoodsOrder;
import com.bicon.botu.mall.order.processor.handler.WalletRpc;
import com.bicon.botu.mall.order.service.IGoodsOrderService;
import com.bicon.botu.core.base.common.controller.BaseController;
import com.bicon.botu.core.base.common.entity.BaseResult;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

/**
 * 前台订单接口
 *
 * @auther 裴志鹏
 * @date 2017/7/20 11:43
 */
@Slf4j
@RestController
@RequestMapping("/V1.0.0/api/goodsOrder")
public class GoodsOrderApi extends BaseController {

    @Autowired
    private IGoodsOrderService iGoodsOrderService;

    @Autowired
    private WalletRpc walletRpc;

    /**
     * 提交订单
     *
     * @param userId                   用户id
     * @param orderAmount              订单总金额
     * @param sourceCode               订单来源
     * @param promotionsDiscountIdJson 优惠券id集合
     * @param expressAmount            邮费
     * @param remark                   买家留言
     * @param goodsIdJson              商品id集合  多个商品使用,号分隔
     * @param goodsNameJson            商品名称集合
     * @param goodsNumberJson          商品数量集合
     * @param skuNameJson              sku名称集合
     * @param itemImageJson            商品列表展示图集合
     * @param goodsPlatformPriceJson   商品价格集合
     * @param name                     收货人
     * @param address                  收货地址
     * @param telephone                联系电话
     * @param flag                     是否从购物车提交订单 0-是 1-否
     * @param goodsFrom                1 商品  2 食物
     * @return
     */
    @RequestMapping(value = "/submitOrder", method = RequestMethod.POST)
    public BaseResult submitOrder(@RequestParam String userId, @RequestParam String orderAmount, @RequestParam String sourceCode, @RequestParam String promotionsDiscountIdJson,
                                  @RequestParam String expressAmount, String remark, @RequestParam String goodsIdJson, @RequestParam String goodsNameJson, @RequestParam String goodsNumberJson,
                                  @RequestParam String skuNameJson, @RequestParam String itemImageJson, @RequestParam String goodsPlatformPriceJson, @RequestParam String name,
                                  @RequestParam String address, @RequestParam String telephone, @RequestParam String flag,@RequestParam String goodsFrom) {
        return iGoodsOrderService.submitOrder(userId, orderAmount, sourceCode, promotionsDiscountIdJson, expressAmount, remark, goodsIdJson, goodsNameJson, goodsNumberJson, skuNameJson, itemImageJson, goodsPlatformPriceJson, name, address, telephone, flag,goodsFrom);
    }

    /**
     * 取消订单
     *
     * @param orderId      订单id
     * @param userId       用户id
     * @param cancelReason 取消订单原因
     * @return
     */
    @RequestMapping(value = "/cancelOrder")
    public BaseResult cancelOrder(@RequestParam String orderId, @RequestParam String userId, @RequestParam String cancelReason) {
        return iGoodsOrderService.cancelOrder(orderId, userId, cancelReason);
    }

    /**
     * 删除订单
     *
     * @param orderId 订单id
     * @param userId  用户id
     * @return
     */
    @RequestMapping(value = "/deleteOrder")
    public BaseResult deleteOrder(@RequestParam String orderId, @RequestParam String userId) {
        return iGoodsOrderService.deleteOrder(orderId, userId);
    }

    /**
     * 我的订单-全部、待付款、代发货、待收货、待评价
     *
     * @param userId  用户id
     * @param status  订单状态
     * @param current 当前页
     * @param size    每页条数
     * @return
     */
    @RequestMapping(value = "/myOrderByStatus")
    public BaseResult myOrder(@RequestParam String userId, String status, @RequestParam int current, @RequestParam int size) {
        return iGoodsOrderService.myOrder(userId, status, current, size);
    }

//    /**
//     * 支付并修改订单状态
//     *
//     * @param orderNo 用户id
//     * @return
//     */
//    @RequestMapping(value = "/updateOrderState")
//    public BaseResult updateOrderState(@RequestParam String orderNo
//                                         /*,@RequestParam("appId") String appId,
//                                       @RequestParam("module") String module,
//                                       @RequestParam("business") String business,
//                                       @RequestParam("payPassword") String payPassword,
//                                       @RequestParam("payType") String payType*/) {
//        GoodsOrder goodsOrder = new GoodsOrder().selectById(orderNo);
//        if (null == goodsOrder) {
//            return sendResult(OrderConstans.RESPONSE_CODE_600, OrderConstans.RESPONSE_MESSAGE_600);
//        }
//        //发起支付 rpc
//        //订单实际支付价格
////        if(goodsOrder.getActualAmount().doubleValue() < 0.01)
////        {
////            return sendResult(400,"订单价格不正确，请确认订单");
////        }
////        String results="";
////        JSONObject jsonObject = new JSONObject();
////        for (int i=0;i<=10;i++){
////            results = walletRpc.checkPayInfo(goodsOrder.getUserId(), payPassword, payType, goodsOrder.getActualAmount().doubleValue(),"1", "order", appId, module, business);
////            jsonObject = JSON.parseObject(results);
////            if((Integer)jsonObject.get("code")!=(OrderConstans.RESPONSE_CODE_404)){
////                break;
////            }
////        }
//        goodsOrder.setStatus(OrderConstans.ORDER_STATUS_08);
//        boolean result = goodsOrder.updateById();
//        if (result) {
//            return sendResult(OrderConstans.RESPONSE_CODE_200, OrderConstans.RESPONSE_MESSAGE_200);
//        }
//
//        return sendResult(OrderConstans.RESPONSE_CODE_600, OrderConstans.RESPONSE_MESSAGE_600);
//    }

}